package com.loan.system.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.loan.common.core.domain.PageQuery;
import com.loan.common.core.page.TableDataInfo;
import com.loan.common.helper.LoginHelper;
import com.loan.common.utils.DateUtils;
import com.loan.common.utils.StringUtils;
import com.loan.system.domain.ApplyEntity;
import com.loan.system.domain.CompanyDataEntity;
import com.loan.system.domain.WithdrawalDataEntity;
import com.loan.system.mapper.ApplyMapper;
import com.loan.system.mapper.WithdrawalDataMapper;
import com.loan.system.service.WithdrawalDataService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class WithdrawalDataServiceImpl implements WithdrawalDataService {

    private final WithdrawalDataMapper withdrawalDataMapper;

    private final ApplyMapper applyMapper;

    @Override
    public TableDataInfo<WithdrawalDataEntity> withdrawalList(String beginTime, String endTime, Long companyId, PageQuery pageQuery) {
        Long cId = LoginHelper.getCompanyId();
        if (ObjectUtil.isNotNull(cId)){
            companyId = cId;
        }
        LambdaQueryWrapper<WithdrawalDataEntity> lqw = new LambdaQueryWrapper<WithdrawalDataEntity>()
                .eq(ObjectUtil.isNotNull(companyId),WithdrawalDataEntity::getCompanyId,companyId)
                .between(StringUtils.isNotBlank(beginTime) && StringUtils.isNotBlank(endTime),
                        WithdrawalDataEntity::getBuildDate, beginTime,endTime)
                .orderByDesc(WithdrawalDataEntity::getBuildDate);
        Page<WithdrawalDataEntity> page = withdrawalDataMapper.selectPage(pageQuery.build(), lqw);
        return TableDataInfo.build(page);
    }
}
